//
//  MainView.m
//  LClass
//
//  Created by 严军 on 16/6/3.
//  Copyright © 2016年 YanJun. All rights reserved.
//

#import "MainView.h"
#import "InfoCellView.h"
#import "SVProgressHUD.h"
@implementation MainView
{
    int hasCheckedPage;//保存翻到的最大页数
    int currentPageNum;//当前页数
    NSMutableArray* backDataList;//存放每次考勤的数据
    UIScrollView* centerScrollView;//中间的横向滑动视图
    UIButton* btnLeftTemp;
    UIButton* btnRigthTemp;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)init
{
    self=[super init];
    if (self) {
        
        
    }
    return self;
}

-(id)initWithDataList:(NSDictionary*)dataDic
{
    self=[super init];
    if (self) {
        _dataInfoDic = dataDic;
        self.backgroundColor=[UIColor whiteColor];
        [self layout];
    }
    return self;
}

-(void)layout
{
    //初始化页码
    hasCheckedPage=1;
    //初始化返回数据的字典
    _backDataDic=[[NSMutableDictionary alloc]initWithDictionary:@{@"allStuNum":[_dataInfoDic objectForKey:@"allStuNum"],//全班总人次
                                                                  @"kaoQinNUm":[_dataInfoDic objectForKey:@"kaoQinNum"],//考勤人次
                                                                  @"vacate":@"",//请假人次
                                                                  @"absentNum":@"",//缺勤人次
                                                                  @"kaoqinData":@""//详细考勤数据
                                                                  }];
    
    //全班人数
    UILabel* lab1=[[UILabel alloc]init];
    _labAllStuNum=lab1;
    [self addSubview:lab1];
    [lab1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(20*SizeScal);
        make.left.equalTo(self).offset(16);
        make.height.mas_equalTo(20*SizeScal);
    }];
    lab1.font=[UIFont systemFontOfSize:14*SizeScal];
    lab1.textColor=[UIColor grayColor];
    
    lab1.text=[NSString stringWithFormat:@"全班人数:%@",[_dataInfoDic objectForKey:@"allStuNum"]];
    
    //考勤人数
    UILabel* lab2=[[UILabel alloc]init];
    _labChooseStuNum=lab2;
    [self addSubview:lab2];
    [lab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab1);
        make.centerX.equalTo(self.mas_centerX);
    }];
    lab2.font=[UIFont systemFontOfSize:14*SizeScal];
    lab2.textColor=[UIColor grayColor];
    lab2.text=[NSString stringWithFormat:@"考勤人数:%@",[_dataInfoDic objectForKey:@"kaoQinNum"]];

    //剩余考勤数
    UILabel* lab4=[[UILabel alloc]init];
    [self addSubview:lab4];
    _labRestStuNum=lab4;
    [lab4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab1);
        make.right.equalTo(self.mas_right).offset(-20);
    }];
    lab4.font=[UIFont boldSystemFontOfSize:30*SizeScal];
    lab4.textColor=[UIColor redColor];
    lab4.text=[NSString stringWithFormat:@"%d",[(NSNumber*)[_dataInfoDic objectForKey:@"kaoQinNum"] intValue]-1];
    
    //还剩:
    UILabel* lab3=[[UILabel alloc]init];
    [self addSubview:lab3];
    [lab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(20*SizeScal);
        make.right.equalTo(lab4.mas_left).offset(-20);
    }];
    lab3.font=[UIFont systemFontOfSize:14*SizeScal];
    lab3.textColor=[UIColor grayColor];
    lab3.text=@"还剩:";

    
 
    //已考勤人数
    UILabel* lab5=[[UILabel alloc]init];
    [self addSubview:lab5];
    [lab5 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lab1.mas_bottom).offset(20*SizeScal);
        make.left.equalTo(lab1);
        make.height.mas_equalTo(20*SizeScal);
    }];
    lab5.font=[UIFont systemFontOfSize:14*SizeScal];
    lab5.textColor=[UIColor grayColor];
    lab5.text=@"已考勤人数";

    
    //已考勤数
    UILabel* lab6=[[UILabel alloc]init];
    [self addSubview:lab6];
    _labCheckedStuNum=lab6;
    [lab6 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab5);
        make.left.equalTo(lab5.mas_right).offset(10);
    }];
    lab6.font=[UIFont boldSystemFontOfSize:20*SizeScal];
    lab6.textColor=TitleColorLight;
    lab6.text=[NSString stringWithFormat:@"%d",hasCheckedPage];
    //结束考勤按钮
    UIButton* btnFinished=[[UIButton alloc]init];
    [self addSubview:btnFinished];
    [btnFinished mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(lab3);
        make.right.equalTo(self).offset(-20);
        make.centerY.equalTo(lab6);
        make.height.mas_equalTo(25*SizeScal);
    }];
    [[Utility sharedInstance] setLayerView:btnFinished borderW:1 borderColor:[UIColor grayColor] radius:8];
    [btnFinished setTitleColor:[UIColor grayColor] forState:0];
    [btnFinished setTitle:@"结束考勤" forState:0];
    btnFinished.titleLabel.font=[UIFont systemFontOfSize:14*SizeScal];
    [btnFinished addTarget:self action:@selector(finishedKaoQin:) forControlEvents:UIControlEventTouchUpInside];
    
    //绘制虚线1
    UIView* dashLine=[[UIView alloc]init];
    [self addSubview:dashLine];
    [dashLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btnFinished.mas_bottom).offset(15*SizeScal);
        make.width.equalTo(self).multipliedBy(0.9);
        make.centerX.equalTo(self);
        make.height.mas_equalTo(1);
    }];
    dashLine.backgroundColor=[UIColor colorWithWhite:0.9 alpha:0.5];
    
    //绘制虚线2
    UIView* dashLineBottom=[[UIView alloc]init];
    [self addSubview:dashLineBottom];
    [dashLineBottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.mas_bottom).offset(-50*SizeScal);
        make.width.equalTo(self).multipliedBy(0.9);
        make.centerX.equalTo(self);
        make.height.mas_equalTo(1);
    }];
    dashLineBottom.backgroundColor=[UIColor colorWithWhite:0.9 alpha:0.5];
    
    //设置左右按钮
    UIButton* btnLeft=[[UIButton alloc]init];
    btnLeftTemp=btnLeft;
    [self addSubview:btnLeft];
    [btnLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dashLineBottom).offset(5*SizeScal);
        make.width.height.mas_equalTo(40*SizeScal);
        make.centerX.equalTo(self).multipliedBy(0.5);
    }];
    [btnLeft setImage:[UIImage imageNamed:@"left"] forState:0];
    btnLeft.tag=100;
    [btnLeft addTarget:self action:@selector(clickPageUpDown:) forControlEvents:UIControlEventTouchUpInside];
    //设置右边按钮
    UIButton* btnRight=[[UIButton alloc]init];
    btnRigthTemp=btnRight;
    [self addSubview:btnRight];
    [btnRight mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dashLineBottom).offset(5*SizeScal);
        make.width.height.mas_equalTo(40*SizeScal);
        make.centerX.equalTo(self).multipliedBy(1.5);
    }];
    btnRight.tag=101;
    [btnRight setImage:[UIImage imageNamed:@"right_light"] forState:0];
    [btnRight addTarget:self action:@selector(clickPageUpDown:) forControlEvents:UIControlEventTouchUpInside];
    //中间的scrollView
    UIScrollView* scrollview=[[UIScrollView alloc]init];
    centerScrollView=scrollview;
    [self addSubview:scrollview];
    [scrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(dashLine.mas_bottom);
        make.bottom.equalTo(dashLineBottom.mas_top);
        make.left.right.equalTo(self);
    }];
    [scrollview setShowsHorizontalScrollIndicator:NO];
    [scrollview setShowsVerticalScrollIndicator:NO];
    [scrollview setPagingEnabled:YES];
    scrollview.backgroundColor=TitleColorLight;
    scrollview.delegate=self;
    
    UIView* contentView=[[UIView alloc]init];
    [scrollview addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(scrollview);
        // 注意到此处的宽度约束条件，这个宽度的约束条件是比添加项
        make.height.equalTo(scrollview);
    }];
    [contentView setUserInteractionEnabled:YES];
    InfoCellView* lasCellV;
    NSArray* dataList=[_dataInfoDic objectForKey:@"kaoQinList"];
    backDataList=[[NSMutableArray alloc]init];
    for (int i=0; i<dataList.count; i++) {
        InfoCellView* cellV=[[InfoCellView alloc]initWithData:[dataList objectAtIndex:i]];
        //cellV.frame=CGRectMake(i*ScreenWidth, 0, ScreenWidth, 300);
        [contentView addSubview:cellV];
        cellV.index=i;//记录顺序
        if (!lasCellV) {
            [cellV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.top.bottom.equalTo(contentView);
                make.width.mas_equalTo(ScreenWidth);
            }];
            lasCellV=cellV;
        }else
        {
            [cellV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.bottom.equalTo(contentView);
                make.left.equalTo(lasCellV.mas_right);
                make.width.mas_equalTo(ScreenWidth);
            }];
            lasCellV=cellV;
        }
        //每初始化一个cellV默认它考勤到位了
        NSMutableDictionary* tempDic=[[NSMutableDictionary alloc] initWithDictionary:@{
                                                                                    @"name:":[[dataList objectAtIndex:i] objectForKey:@"studentName"],//姓名
                                                                                      @"stuNum":[[dataList objectAtIndex:i] objectForKey:@"stunumber"],//学号
                                                                                      @"state":@"1",//考勤状态（默认为1）
                                                                                    @"studentId":[[dataList objectAtIndex:i] objectForKey:@"studentId"],
                                                                                    @"courseId":[[dataList objectAtIndex:i] objectForKey:@"courseId"]
                                                                                    }];//;
        [backDataList addObject:tempDic];
        cellV.kaoQinBlock=^(NSDictionary* dic){
            int index=  [(NSNumber*)[dic objectForKey:@"index"] intValue];
            //返回考勤情况处理
           [[backDataList objectAtIndex:index] setObject:[dic objectForKey:@"state"] forKey:@"state"];
        };
    }
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(lasCellV.mas_right);
    }];
    
    }

#pragma mark ScrollView-Delegat

- (void) scrollViewDidScroll:(UIScrollView *)sender {
    // 得到每页宽度
    CGFloat pageWidth = sender.frame.size.width;
    // 根据当前的x坐标和页宽度计算出当前页数
    int currentPage = floor((sender.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    currentPageNum=currentPage;
    if((currentPage+1)>hasCheckedPage)
    {
        //刷新已考勤人数
        hasCheckedPage=currentPage+1;
        _labCheckedStuNum.text=[NSString stringWithFormat:@"%d",hasCheckedPage];
        _labRestStuNum.text=[NSString stringWithFormat:@"%lu",((NSArray*)[_dataInfoDic objectForKey:@"kaoQinList"]).count-hasCheckedPage];
    }
    
    //设置按钮颜色
    if(currentPage==0)
    {
        [btnLeftTemp setImage:[UIImage imageNamed:@"left"] forState:0];
    }
    else if (currentPage==((int)[[_dataInfoDic objectForKey:@"kaoQinList"] count]-1)) {
        [btnLeftTemp setImage:[UIImage imageNamed:@"left_light"] forState:0];
        [btnRigthTemp setImage:[UIImage imageNamed:@"right"] forState:0];
    }else
    {
        [btnLeftTemp setImage:[UIImage imageNamed:@"left_light"] forState:0];
        [btnRigthTemp setImage:[UIImage imageNamed:@"right_light"] forState:0];
    }
}

//结束考勤
-(void)finishedKaoQin:(UIButton*)btn
{
    int absentNum=0;//缺勤人次
    int vacate=0;//请假人次
    for (int i=0; i<backDataList.count; i++) {
        int state=[(NSNumber*)[[backDataList objectAtIndex:i] objectForKey:@"state"] intValue];
        if (state==2) {
            //缺勤
            absentNum++;
        }else if(state==3)
        {
            //请假
            vacate++;
        }
    }
    
    [_backDataDic setObject:[NSString stringWithFormat:@"%d",absentNum] forKey:@"absentNum"];
    [_backDataDic setObject:[NSString stringWithFormat:@"%d",vacate] forKey:@"vacate"];
    [_backDataDic setObject:backDataList forKey:@"kaoqinData"];
    _finishiBlock(_backDataDic);
}

//左右按钮翻页
-(void)clickPageUpDown:(id)sender
{
    int allNumber = (int)[[_dataInfoDic objectForKey:@"kaoQinList"] count];//总考勤人数（相当于页数）
    UIButton* btntemp=(UIButton*)sender;
    if (btntemp.tag==100) {
        //点击左边
        if (currentPageNum>0) {
            [centerScrollView setContentOffset:CGPointMake((currentPageNum-1)*ScreenWidth, 0) animated:YES];
        }
        
    }else if(btntemp.tag==101)
    {
        //点击右边
        if(currentPageNum<allNumber-1)
        {
            [centerScrollView setContentOffset:CGPointMake((currentPageNum+1)*ScreenWidth, 0) animated:YES];
        }else
        {
            [SVProgressHUD showInfoWithStatus:@"别翻了，到底了！"];
        }
        
    }
}

@end
